﻿using System.Windows;
using Microsoft.Practices.Composite.Events;
using Airborne.ScrumPoker.Module.Events;
using Microsoft.Practices.Composite.Wpf.Events;

namespace Airborne.ScrumPoker.Presentation.ProductOwner
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Shell : Window
    {
        private IEventAggregator EventAggregator { get; set; }

        private double TfsGridColumnExpandToSize;

        public Shell(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            InitializeComponent();
            SubscribeToEvents();
        }

        private void SubscribeToEvents()
        {
            EventAggregator.GetEvent<GenericEvent<string>>().Subscribe(ShowErrorMessage, ThreadOption.UIThread, true, p => p.Topic == EventTopics.Error);
            EventAggregator.GetEvent<GenericEvent<string>>().Subscribe(PerformControlResize, ThreadOption.UIThread, true, p => p.Topic == EventTopics.ResizeMainRegionContent);
        }

        private void ShowErrorMessage(EventParameters<string> parameters)
        {
            if (!string.IsNullOrEmpty(parameters.Value))
            {
                MessageBox.Show(this, parameters.Value, Application.Current.MainWindow.Title);
            }
        }

        private void mainRegionStackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ResizeContentControl(e.NewSize.Width, e.NewSize.Height);
        }
        
        private void PerformControlResize(EventParameters<string> parameters)
        {
            ResizeContentControl(this.mainRegionStackPanel.Width, this.mainRegionStackPanel.Height);    
        }

        private void ResizeContentControl(double width, double height)
        {
            this.mainRegionContent.Width = width;
            this.mainRegionContent.Height = height;

            if (this.mainRegionContent.HasContent)
            {
                var control = this.mainRegionContent.Content as System.Windows.Controls.UserControl;
                control.Width = this.mainRegionContent.Width;
                control.Height = this.mainRegionContent.Height;
            }

            RecordGridPanelExpand();
        }


        private void RecordGridPanelExpand()
        {
            var targetPanel = this.containerGrid.ColumnDefinitions[0];

            if (targetPanel.Width.Value > 0)
            {
                TfsGridColumnExpandToSize = this.containerGrid.ColumnDefinitions[0].Width.Value;
            }
        }

        private void GridSplitter_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var targetPanel = this.containerGrid.ColumnDefinitions[0];

            if (targetPanel.Width.Value == 0)
            {
                targetPanel.Width = new GridLength(TfsGridColumnExpandToSize);
            }
            else
            {
                targetPanel.Width = new GridLength(0);
            }
        }
 
    }
}
